package com.dcy.znoa.controller.sys;

import com.dcy.znoa.common.annotation.AuthenticationAPI;
import com.dcy.znoa.common.controller.BaseController;
import com.dcy.znoa.common.exception.BusinessException;
import com.dcy.znoa.common.util.pojo.JsonCommon;
import com.dcy.znoa.common.util.pojo.JsonForList;
import com.dcy.znoa.controller.sys.model.storeFile.AddStoreFileModel;
import com.dcy.znoa.controller.sys.model.storeFile.SelectStoreFileModel;
import com.dcy.znoa.domain.Oper;
import com.dcy.znoa.domain.sys.StoreFile;
import com.dcy.znoa.service.sys.StoreFileService;

import cn.jpush.api.utils.StringUtils;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.validation.Valid;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * @author chenjie
 */
@Controller
@RequestMapping("stroeFile")
public class StoreFileController extends BaseController {
    @Autowired
    StoreFileService storeFileService;

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/selectList")
    public JsonForList selectList(SelectStoreFileModel model) throws BusinessException, InvocationTargetException, IllegalAccessException {
        return storeFileService.selectList(model);
    }

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/insert")
    public JsonCommon insert(@Valid AddStoreFileModel model) throws BusinessException, InvocationTargetException, IllegalAccessException {
    	Oper oper = getCurrentUser();
    	model.setCreateOperId(oper.getOperId());    
        
    	String[] fileNames = model.getRemoteFileName().split("\\,");
    	
    	for (String remoteFileName : fileNames) {
    		StoreFile storeFile = new StoreFile();
            BeanUtils.copyProperties(storeFile, model);
            storeFile.setRemoteFileName(remoteFileName);
            storeFileService.insert(storeFile);
		}

        return new JsonCommon(JsonCommon.SUCCESS_CODE, JsonCommon.SUCCESS_MSG);
    }
    
    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/update")
    public JsonCommon update(@Valid AddStoreFileModel model,@RequestParam String delRemoteFileNames) throws BusinessException, InvocationTargetException, IllegalAccessException {
     	//先删除
    	String[] delFileNames = delRemoteFileNames.split("\\,");
    	for (String delFileName : delFileNames) {
    		storeFileService.updateByEntityId(model.getEntityId(),model.getEntityTbl(),delFileName);
		}
    	    	
    	//然后增加
    	Oper oper = getCurrentUser();
    	model.setCreateOperId(oper.getOperId());    
        
    	String[] fileNames = model.getRemoteFileName().split("\\,");
    	
    	for (String remoteFileName : fileNames) {
    		StoreFile storeFile = new StoreFile();
            BeanUtils.copyProperties(storeFile, model);
            storeFile.setRemoteFileName(remoteFileName);
            storeFileService.insert(storeFile);
		}

        return new JsonCommon(JsonCommon.SUCCESS_CODE, JsonCommon.SUCCESS_MSG);
    }

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/selectStoreFileByEntityId")
    public JsonForList selectStoreFileByEntityId(@RequestParam Long entityId,@RequestParam String entityTbl) throws BusinessException, InvocationTargetException, IllegalAccessException {
    	List<StoreFile> list = storeFileService.selectStoreFileByEntityId(entityId,entityTbl,null);
    	return new JsonForList(list,JsonCommon.SUCCESS_CODE, JsonCommon.SUCCESS_MSG);
    }
    
    
    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/logicDeleteByEntityId")
    public JsonCommon logicDeleteByEntityId(@RequestParam Long entityId,@RequestParam String entityTbl) throws BusinessException, InvocationTargetException, IllegalAccessException {
        return storeFileService.updateByEntityId(entityId,entityTbl);
    }
}
